<template>
  <div>
    <e-table
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :pageObj="null"
      @row-click="handleRowClick"
    >
      <el-table-column slot="sort" label="排序" width="100" align="center">
        <template slot-scope="scope">
          <el-input
            style="width: 65px"
            :max="100"
            size="mini"
            v-model="scope.row.menuOrder"
            @blur="handleOrderChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column slot="state" width="80" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state === 0">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="显示名称" slot="title" align="center">
        <template slot-scope="scope">{{ JSON.parse(scope.row.ext).title }}</template>
      </el-table-column>
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { getMenu, deleteMenu, getMenuById, getMenuAll } from '@/api/menu'
import { openNotification, orderChange } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      // 查询参数
      queryParam: {
        id: '',
        pageNo: 1,
        pageSize: 10
      },
      // 表头
      columns: [
        {
          label: '菜单名称',
          prop: 'menuName'
        },
        {
          slot: 'title'
        },
        {
          label: 'ICON',
          prop: 'ico'
        },
        {
          label: '路径',
          prop: 'url'
        },
        {
          slot: 'state'
        },
        {
          slot: 'sort'
        },
        {
          slot: 'action'
        }
      ],
      tableData: [],
      selectObj: {},
      total: 1
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handleRowClick(row, column, event) {
      console.log('row', row)
      let obj = {}
      obj.parentName = row.showName
      obj.parentId = row.menuId
      this.$emit('setTree', obj)
    },
    getId(obj) {
      console.log('==========点击')
      this.selectObj = obj
      this.queryParam.id = obj.parentId
      this.queryParam.pageNo = 1
      this.handleGetList()
    },
    async handleOrderChange(row) {
      const res = await orderChange(row, 'menuOrder', 'menuId', 'menu')
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(row) {
      const { parentName, parentId } = this.selectObj
      this.$nextTick(() => {
        const obj = Object.assign(row, { parentName, parentId })
        this.$emit('edit', obj)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.menuName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteMenu(row.menuId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getMenuById(this.queryParam)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
        // this.total = total
      } else {
        this.tableData = []
        // this.total = 1
      }
      this.loading = false
    }
  }
}
</script>
